home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / tutorials / custEducation / opengl1 / lib / shapes.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  28.9 KB  |  1,089 lines

  1. /*
  2.  * (c) Copyright 1993, 1994, Silicon Graphics, Inc.
  3.  * ALL RIGHTS RESERVED 
  4.  * Permission to use, copy, modify, and distribute this software for 
  5.  * any purpose and without fee is hereby granted, provided that the above
  6.  * copyright notice appear in all copies and that both the copyright notice
  7.  * and this permission notice appear in supporting documentation, and that 
  8.  * the name of Silicon Graphics, Inc. not be used in advertising
  9.  * or publicity pertaining to distribution of the software without specific,
  10.  * written prior permission. 
  11.  *
  12.  * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
  13.  * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
  14.  * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
  15.  * FITNESS FOR A PARTICULAR PURPOSE.  IN NO EVENT SHALL SILICON
  16.  * GRAPHICS, INC.  BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
  17.  * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
  18.  * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
  19.  * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
  20.  * THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC.  HAS BEEN
  21.  * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
  22.  * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
  23.  * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
  24.  * 
  25.  * US Government Users Restricted Rights 
  26.  * Use, duplication, or disclosure by the Government is subject to
  27.  * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
  28.  * (c)(1)(ii) of the Rights in Technical Data and Computer Software
  29.  * clause at DFARS 252.227-7013 and/or in similar or successor
  30.  * clauses in the FAR or the DOD or NASA FAR Supplement.
  31.  * Unpublished-- rights reserved under the copyright laws of the
  32.  * United States.  Contractor/manufacturer is Silicon Graphics,
  33.  * Inc., 2011 N.  Shoreline Blvd., Mountain View, CA 94039-7311.
  34.  *
  35.  * OpenGL(TM) is a trademark of Silicon Graphics, Inc.
  36.  */
  37. #include <stdio.h>
  38. #include <math.h>
  39. #include <GL/gl.h>
  40. #include <GL/glu.h>
  41. #include "aux.h"
  42. #include "3d.h"
  43.  
  44. #define SPHEREWIRE    0
  45. #define CUBEWIRE    1
  46. #define BOXWIRE        2
  47. #define TORUSWIRE    3
  48. #define CYLINDERWIRE    4
  49. #define ICOSAWIRE    5
  50. #define OCTAWIRE    6
  51. #define TETRAWIRE    7
  52. #define DODECAWIRE    8
  53. #define CONEWIRE    9
  54. #define SPHERESOLID    10
  55. #define CUBESOLID    11
  56. #define BOXSOLID    12
  57. #define TORUSSOLID    13
  58. #define CYLINDERSOLID    14
  59. #define ICOSASOLID    15
  60. #define OCTASOLID    16
  61. #define TETRASOLID    17
  62. #define DODECASOLID    18
  63. #define CONESOLID    19
  64.  
  65. #define PI 3.1415926535897
  66.  
  67. /*    structure for each geometric object    */
  68. typedef struct model {
  69.     GLuint list;    /*  display list to render object   */
  70.     struct model *ptr;    /*  pointer to next object    */
  71.     int numParam;    /*  # of parameters        */
  72.     GLdouble *params;    /*  array with parameters    */
  73. } MODEL, *MODELPTR;
  74.  
  75. /*    array of linked lists--used to keep track of display lists 
  76.  *    for each different type of geometric object.
  77.  */
  78. static MODELPTR lists[25] = {
  79.     NULL, NULL, NULL, NULL, NULL,
  80.     NULL, NULL, NULL, NULL, NULL,
  81.     NULL, NULL, NULL, NULL, NULL,
  82.     NULL, NULL, NULL, NULL, NULL,
  83.     NULL, NULL, NULL, NULL, NULL
  84. };
  85.  
  86. GLuint findList (int index, GLdouble *paramArray, int size);
  87. int compareParams (GLdouble *oneArray, GLdouble *twoArray, int size);
  88. GLuint makeModelPtr (int index, GLdouble *sizeArray, int count);
  89.  
  90. static void drawbox(GLdouble, GLdouble, GLdouble, 
  91.     GLdouble, GLdouble, GLdouble, GLenum);
  92. static void doughnut(GLdouble, GLdouble, GLint, GLint, GLenum);
  93. static void icosahedron(GLdouble *, GLdouble, GLenum);
  94. static void octahedron(GLdouble *, GLdouble, GLenum);
  95. static void tetrahedron(GLdouble *, GLdouble, GLenum);
  96. static void subdivide(int, GLdouble *, GLdouble *, GLdouble *,
  97.     GLdouble *, GLdouble, GLenum, int);
  98. static void drawtriangle(int, int, int,
  99.     GLdouble *, GLdouble, GLenum, int);
  100. static void recorditem(GLdouble *, GLdouble *, GLdouble *,
  101.     GLdouble *, GLdouble, GLenum, int);
  102. static void initdodec(void);
  103. static void dodecahedron(GLdouble *, GLdouble, GLenum);
  104. static void pentagon(int, int, int, int, int, GLenum);
  105.  
  106.  
  107. /*  Render wire frame or solid sphere.  If no display list with
  108.  *  the current model size exists, create a new display list.
  109.  */
  110. void auxWireSphere (GLdouble radius)
  111. {
  112.     GLUquadricObj *quadObj;
  113.     GLdouble *sizeArray;
  114.     GLuint displayList;
  115.  
  116.     sizeArray = (GLdouble *) malloc (sizeof (GLdouble) * 1);
  117.     *sizeArray = radius;
  118.     displayList = findList (SPHEREWIRE, sizeArray, 1);
  119.  
  120.     if (displayList == 0) {
  121.     glNewList(makeModelPtr (SPHEREWIRE, sizeArray, 1),
  122.         GL_COMPILE_AND_EXECUTE);
  123.         quadObj = gluNewQuadric ();
  124.         gluQuadricDrawStyle (quadObj, GLU_LINE);
  125.         gluSphere (quadObj, radius, 16, 16);
  126.     glEndList();
  127.     }
  128.     else {
  129.     glCallList(displayList);
  130.     free (sizeArray);
  131.     }
  132. }
  133.  
  134. void auxSolidSphere (GLdouble radius)
  135. {
  136.     GLUquadricObj *quadObj;
  137.     GLdouble *sizeArray;
  138.     GLuint displayList;
  139.  
  140.     sizeArray = (GLdouble *) malloc (sizeof (GLdouble) * 1);
  141.     *sizeArray = radius;
  142.     displayList = findList (SPHERESOLID, sizeArray, 1);
  143.  
  144.     if (displayList == 0) {
  145.     glNewList(makeModelPtr (SPHERESOLID, sizeArray, 1),
  146.         GL_COMPILE_AND_EXECUTE);
  147.         quadObj = gluNewQuadric ();
  148.         gluQuadricDrawStyle (quadObj, GLU_FILL);
  149.         gluQuadricNormals (quadObj, GLU_SMOOTH);
  150.         gluSphere (quadObj, radius, 16, 16);
  151.     glEndList();
  152.     }
  153.     else {
  154.     glCallList(displayList);
  155.     free (sizeArray);
  156.     }
  157. }
  158.  
  159. /*  Render wire frame or solid cube.  If no display list with
  160.  *  the current model size exists, create a new display list.
  161.  */
  162. void auxWireCube (GLdouble size)
  163. {
  164.     GLdouble *sizeArray;
  165.     GLuint displayList;
  166.  
  167.     sizeArray = (GLdouble *) malloc (sizeof (GLdouble) * 1);
  168.     *sizeArray = size;
  169.     displayList = findList (CUBEWIRE, sizeArray, 1);
  170.  
  171.     if (displayList == 0) {
  172.     glNewList(makeModelPtr (CUBEWIRE, sizeArray, 1),
  173.         GL_COMPILE_AND_EXECUTE);
  174.         drawbox(-size/2., size/2., -size/2., size/2., 
  175.         -size/2., size/2., GL_LINE_LOOP);
  176.     glEndList();
  177.     }
  178.     else {
  179.     glCallList(displayList);
  180.     free (sizeArray);
  181.     }
  182. }
  183.  
  184. void auxSolidCube (GLdouble size)
  185. {
  186.     GLdouble *sizeArray;
  187.     GLuint displayList;
  188.  
  189.     sizeArray = (GLdouble *) malloc (sizeof (GLdouble) * 1);
  190.     *sizeArray = size;
  191.     displayList = findList (CUBESOLID, sizeArray, 1);
  192.  
  193.     if (displayList == 0) {
  194.     glNewList(makeModelPtr (CUBESOLID, sizeArray, 1),
  195.         GL_COMPILE_AND_EXECUTE);
  196.         drawbox(-size/2., size/2., -size/2., size/2., 
  197.         -size/2., size/2., GL_QUADS);
  198.     glEndList();
  199.     }
  200.     else {
  201.     glCallList(displayList);
  202.     free (sizeArray);
  203.     }
  204. }
  205.  
  206. /*  Render wire frame or solid cube.  If no display list with
  207.  *  the current model size exists, create a new display list.
  208.  */
  209. void auxWireBox (GLdouble width, GLdouble height, GLdouble depth)
  210. {
  211.     GLdouble *sizeArray, *tmp;
  212.     GLuint displayList;
  213.  
  214.     sizeArray = (GLdouble *) malloc (sizeof (GLdouble) * 3);
  215.     tmp = sizeArray;
  216.     *tmp++ = width;
  217.     *tmp++ = height;
  218.     *tmp++ = depth;
  219.     displayList = findList (BOXWIRE, sizeArray, 3);
  220.  
  221.     if (displayList == 0) {
  222.     glNewList(makeModelPtr (BOXWIRE, sizeArray, 3),
  223.         GL_COMPILE_AND_EXECUTE);
  224.         drawbox(-width/2., width/2., -height/2., height/2., 
  225.         -depth/2., depth/2., GL_LINE_LOOP);
  226.     glEndList();
  227.     }
  228.     else {
  229.     glCallList(displayList);
  230.     free (sizeArray);
  231.     }
  232. }
  233.  
  234. void auxSolidBox (GLdouble width, GLdouble height, GLdouble depth)
  235. {
  236.     GLdouble *sizeArray, *tmp;
  237.     GLuint displayList;
  238.  
  239.     sizeArray = (GLdouble *) malloc (sizeof (GLdouble) * 3);
  240.     tmp = sizeArray;
  241.     *tmp++ = width;
  242.     *tmp++ = height;
  243.     *tmp++ = depth;
  244.     displayList = findList (BOXSOLID, sizeArray, 3);
  245.  
  246.     if (displayList == 0) {
  247.     glNewList(makeModelPtr (BOXSOLID, sizeArray, 3),
  248.         GL_COMPILE_AND_EXECUTE);
  249.         drawbox(-width/2., width/2., -height/2., height/2., 
  250.         -depth/2., depth/2., GL_QUADS);
  251.     glEndList();
  252.     }
  253.     else {
  254.     glCallList(displayList);
  255.     free (sizeArray);
  256.     }
  257. }
  258.  
  259. /*  Render wire frame or solid tori.  If no display list with
  260.  *  the current model size exists, create a new display list.
  261.  */
  262. void auxWireTorus (GLdouble innerRadius, GLdouble outerRadius)
  263. {
  264.     GLdouble *sizeArray, *tmp;
  265.     GLuint displayList;
  266.  
  267.     sizeArray = (GLdouble *) malloc (sizeof (GLdouble) * 2);
  268.     tmp = sizeArray;
  269.     *tmp++ = innerRadius;
  270.     *tmp++ = outerRadius;
  271.     displayList = findList (TORUSWIRE, sizeArray, 2);
  272.  
  273.     if (displayList == 0) {
  274.     glNewList(makeModelPtr (TORUSWIRE, sizeArray, 2),
  275.         GL_COMPILE_AND_EXECUTE);
  276.         doughnut(innerRadius, outerRadius, 5, 10, GL_LINE_LOOP);
  277.     glEndList();
  278.     }
  279.     else {
  280.     glCallList(displayList);
  281.     free (sizeArray);
  282.     }
  283. }
  284.  
  285. void auxSolidTorus (GLdouble innerRadius, GLdouble outerRadius)
  286. {
  287.     GLdouble *sizeArray, *tmp;
  288.     GLuint displayList;
  289.  
  290.     sizeArray = (GLdouble *) malloc (sizeof (GLdouble) * 2);
  291.     tmp = sizeArray;
  292.     *tmp++ = innerRadius;
  293.     *tmp++ = outerRadius;
  294.     displayList = findList (TORUSSOLID, sizeArray, 2);
  295.  
  296.     if (displayList == 0) {
  297.     glNewList(makeModelPtr (TORUSSOLID, sizeArray, 2),
  298.         GL_COMPILE_AND_EXECUTE);
  299.         doughnut(innerRadius, outerRadius, 8, 15, GL_QUADS);
  300.     glEndList();
  301.     }
  302.     else {
  303.     glCallList(displayList);
  304.     free (sizeArray);
  305.     }
  306. }
  307.  
  308. /*  Render wire frame or solid cylinders.  If no display list with
  309.  *  the current model size exists, create a new display list.
  310.  */
  311. void auxWireCylinder (GLdouble radius, GLdouble height)
  312. {
  313.     GLUquadricObj *quadObj;
  314.     GLdouble *sizeArray, *tmp;
  315.     GLuint displayList;
  316.  
  317.     sizeArray = (GLdouble *) malloc (sizeof (GLdouble) * 2);
  318.     tmp = sizeArray;
  319.     *tmp++ = radius;
  320.     *tmp++ = height;
  321.     displayList = findList (CYLINDERWIRE, sizeArray, 2);
  322.  
  323.     if (displayList == 0) {
  324.     glNewList(makeModelPtr (CYLINDERWIRE, sizeArray, 2),
  325.         GL_COMPILE_AND_EXECUTE);
  326.         glPushMatrix ();
  327.         glRotatef (90.0, 1.0, 0.0, 0.0);
  328.         glTranslatef (0.0, 0.0, -1.0);
  329.         quadObj = gluNewQuadric ();
  330.         gluQuadricDrawStyle (quadObj, GLU_LINE);
  331.         gluCylinder (quadObj, radius, radius, height, 12, 2);
  332.         glPopMatrix ();
  333.     glEndList();
  334.     }
  335.     else {
  336.     glCallList(displayList);
  337.     free (sizeArray);
  338.     }
  339. }
  340.  
  341. void auxSolidCylinder (GLdouble radius, GLdouble height)
  342. {
  343.     GLUquadricObj *quadObj;
  344.     GLdouble *sizeArray, *tmp;
  345.     GLuint displayList;
  346.  
  347.     sizeArray = (GLdouble *) malloc (sizeof (GLdouble) * 2);
  348.     tmp = sizeArray;
  349.     *tmp++ = radius;
  350.     *tmp++ = height;
  351.     displayList = findList (CYLINDERSOLID, sizeArray, 2);
  352.  
  353.     if (displayList == 0) {
  354.     glNewList(makeModelPtr (CYLINDERSOLID, sizeArray, 2),
  355.         GL_COMPILE_AND_EXECUTE);
  356.         glPushMatrix ();
  357.         glRotatef (90.0, 1.0, 0.0, 0.0);
  358.         glTranslatef (0.0, 0.0, -1.0);
  359.         quadObj = gluNewQuadric ();
  360.         gluQuadricDrawStyle (quadObj, GLU_FILL);
  361.         gluQuadricNormals (quadObj, GLU_SMOOTH);
  362.         gluCylinder (quadObj, radius, radius, height, 12, 2);
  363.         glPopMatrix ();
  364.     glEndList();
  365.     }
  366.     else {
  367.     glCallList(displayList);
  368.     free (sizeArray);
  369.     }
  370. }
  371.  
  372. /*  Render wire frame or solid icosahedra.  If no display list with
  373.  *  the current model size exists, create a new display list.
  374.  */
  375. void auxWireIcosahedron (GLdouble radius)
  376. {
  377.     GLdouble *sizeArray;
  378.     GLuint displayList;
  379.     GLdouble center[3] = {0.0, 0.0, 0.0};
  380.  
  381.     sizeArray = (GLdouble *) malloc (sizeof (GLdouble) * 1);
  382.     *sizeArray = radius;
  383.     displayList = findList (ICOSAWIRE, sizeArray, 1);
  384.  
  385.     if (displayList == 0) {
  386.     glNewList(makeModelPtr (ICOSAWIRE, sizeArray, 1),
  387.         GL_COMPILE_AND_EXECUTE);
  388.         icosahedron (center, radius, GL_LINE_LOOP);
  389.     glEndList();
  390.     }
  391.     else {
  392.     glCallList(displayList);
  393.     free (sizeArray);
  394.     }
  395. }
  396.  
  397. void auxSolidIcosahedron (GLdouble radius)
  398. {
  399.     GLdouble *sizeArray;
  400.     GLuint displayList;
  401.     GLdouble center[3] = {0.0, 0.0, 0.0};
  402.  
  403.     sizeArray = (GLdouble *) malloc (sizeof (GLdouble) * 1);
  404.     *sizeArray = radius;
  405.     displayList = findList (ICOSASOLID, sizeArray, 1);
  406.  
  407.     if (displayList == 0) {
  408.     glNewList(makeModelPtr (ICOSASOLID, sizeArray, 1),
  409.         GL_COMPILE_AND_EXECUTE);
  410.         icosahedron (center, radius, GL_TRIANGLES);
  411.     glEndList();
  412.     }
  413.     else {
  414.     glCallList(displayList);
  415.     free (sizeArray);
  416.     }
  417. }
  418.  
  419. /*  Render wire frame or solid octahedra.  If no display list with
  420.  *  the current model size exists, create a new display list.
  421.  */
  422. void auxWireOctahedron (GLdouble radius)
  423. {
  424.     GLdouble *sizeArray;
  425.     GLuint displayList;
  426.     GLdouble center[3] = {0.0, 0.0, 0.0};
  427.  
  428.     sizeArray = (GLdouble *) malloc (sizeof (GLdouble) * 1);
  429.     *sizeArray = radius;
  430.     displayList = findList (OCTAWIRE, sizeArray, 1);
  431.  
  432.     if (displayList == 0) {
  433.     glNewList(makeModelPtr (OCTAWIRE, sizeArray, 1),
  434.         GL_COMPILE_AND_EXECUTE);
  435.         octahedron (center, radius, GL_LINE_LOOP);
  436.     glEndList();
  437.     }
  438.     else {
  439.     glCallList(displayList);
  440.     free (sizeArray);
  441.     }
  442. }
  443.  
  444. void auxSolidOctahedron (GLdouble radius)
  445. {
  446.     GLdouble *sizeArray;
  447.     GLuint displayList;
  448.     GLdouble center[3] = {0.0, 0.0, 0.0};
  449.  
  450.     sizeArray = (GLdouble *) malloc (sizeof (GLdouble) * 1);
  451.     *sizeArray = radius;
  452.     displayList = findList (OCTASOLID, sizeArray, 1);
  453.  
  454.     if (displayList == 0) {
  455.     glNewList(makeModelPtr (OCTASOLID, sizeArray, 1),
  456.         GL_COMPILE_AND_EXECUTE);
  457.         octahedron (center, radius, GL_TRIANGLES);
  458.     glEndList();
  459.     }
  460.     else {
  461.     glCallList(displayList);
  462.     free (sizeArray);
  463.     }
  464. }
  465.  
  466. /*  Render wire frame or solid tetrahedra.  If no display list with
  467.  *  the current model size exists, create a new display list.
  468.  */
  469. void auxWireTetrahedron (GLdouble radius)
  470. {
  471.     GLdouble *sizeArray;
  472.     GLuint displayList;
  473.     GLdouble center[3] = {0.0, 0.0, 0.0};
  474.  
  475.     sizeArray = (GLdouble *) malloc (sizeof (GLdouble) * 1);
  476.     *sizeArray = radius;
  477.     displayList = findList (TETRAWIRE, sizeArray, 1);
  478.  
  479.     if (displayList == 0) {
  480.     glNewList(makeModelPtr (TETRAWIRE, sizeArray, 1),
  481.         GL_COMPILE_AND_EXECUTE);
  482.         tetrahedron (center, radius, GL_LINE_LOOP);
  483.     glEndList();
  484.     }
  485.     else {
  486.     glCallList(displayList);
  487.     free (sizeArray);
  488.     }
  489. }
  490.  
  491. void auxSolidTetrahedron (GLdouble radius)
  492. {
  493.     GLdouble *sizeArray;
  494.     GLuint displayList;
  495.     GLdouble center[3] = {0.0, 0.0, 0.0};
  496.  
  497.     sizeArray = (GLdouble *) malloc (sizeof (GLdouble) * 1);
  498.     *sizeArray = radius;
  499.     displayList = findList (TETRASOLID, sizeArray, 1);
  500.  
  501.     if (displayList == 0) {
  502.     glNewList(makeModelPtr (TETRASOLID, sizeArray, 1),
  503.         GL_COMPILE_AND_EXECUTE);
  504.         tetrahedron (center, radius, GL_TRIANGLES);
  505.     glEndList();
  506.     }
  507.     else {
  508.     glCallList(displayList);
  509.     free (sizeArray);
  510.     }
  511. }
  512.  
  513. /*  Render wire frame or solid dodecahedra.  If no display list with
  514.  *  the current model size exists, create a new display list.
  515.  */
  516. void auxWireDodecahedron (GLdouble radius)
  517. {
  518.     GLdouble *sizeArray;
  519.     GLuint displayList;
  520.     GLdouble center[3] = {0.0, 0.0, 0.0};
  521.  
  522.     sizeArray = (GLdouble *) malloc (sizeof (GLdouble) * 1);
  523.     *sizeArray = radius;
  524.     displayList = findList (DODECAWIRE, sizeArray, 1);
  525.  
  526.     if (displayList == 0) {
  527.     glNewList(makeModelPtr (DODECAWIRE, sizeArray, 1),
  528.         GL_COMPILE_AND_EXECUTE);
  529.         dodecahedron (center, radius/1.73, GL_LINE_LOOP);
  530.     glEndList();
  531.     }
  532.     else {
  533.     glCallList(displayList);
  534.     free (sizeArray);
  535.     }
  536. }
  537.  
  538. void auxSolidDodecahedron (GLdouble radius)
  539. {
  540.     GLdouble *sizeArray;
  541.     GLuint displayList;
  542.     GLdouble center[3] = {0.0, 0.0, 0.0};
  543.  
  544.     sizeArray = (GLdouble *) malloc (sizeof (GLdouble) * 1);
  545.     *sizeArray = radius;
  546.     displayList = findList (DODECASOLID, sizeArray, 1);
  547.  
  548.     if (displayList == 0) {
  549.     glNewList(makeModelPtr (DODECASOLID, sizeArray, 1),
  550.         GL_COMPILE_AND_EXECUTE);
  551.         dodecahedron (center, radius/1.73, GL_TRIANGLE_FAN);
  552.     glEndList();
  553.     }
  554.     else {
  555.     glCallList(displayList);
  556.     free (sizeArray);
  557.     }
  558. }
  559.  
  560. /*  Render wire frame or solid cones.  If no display list with
  561.  *  the current model size exists, create a new display list.
  562.  */
  563. void auxWireCone (GLdouble base, GLdouble height)
  564. {
  565.     GLUquadricObj *quadObj;
  566.     GLdouble *sizeArray, *tmp;
  567.     GLuint displayList;
  568.  
  569.     sizeArray = (GLdouble *) malloc (sizeof (GLdouble) * 2);
  570.     tmp = sizeArray;
  571.     *tmp++ = base;
  572.     *tmp++ = height;
  573.     displayList = findList (CONEWIRE, sizeArray, 2);
  574.  
  575.     if (displayList == 0) {
  576.     glNewList(makeModelPtr (CONEWIRE, sizeArray, 2),
  577.         GL_COMPILE_AND_EXECUTE);
  578.         quadObj = gluNewQuadric ();
  579.         gluQuadricDrawStyle (quadObj, GLU_LINE);
  580.         gluCylinder (quadObj, base, 0.0, height, 15, 10);
  581.     glEndList();
  582.     }
  583.     else {
  584.     glCallList(displayList);
  585.     free (sizeArray);
  586.     }
  587. }
  588.  
  589. void auxSolidCone (GLdouble base, GLdouble height)
  590. {
  591.     GLUquadricObj *quadObj;
  592.     GLdouble *sizeArray, *tmp;
  593.     GLuint displayList;
  594.  
  595.     sizeArray = (GLdouble *) malloc (sizeof (GLdouble) * 2);
  596.     tmp = sizeArray;
  597.     *tmp++ = base;
  598.     *tmp++ = height;
  599.     displayList = findList (CONESOLID, sizeArray, 2);
  600.  
  601.     if (displayList == 0) {
  602.     glNewList(makeModelPtr (CONESOLID, sizeArray, 2),
  603.         GL_COMPILE_AND_EXECUTE);
  604.         quadObj = gluNewQuadric ();
  605.         gluQuadricDrawStyle (quadObj, GLU_FILL);
  606.         gluQuadricNormals (quadObj, GLU_SMOOTH);
  607.         gluCylinder (quadObj, base, 0.0, height, 15, 10);
  608.     glEndList();
  609.     }
  610.     else {
  611.     glCallList(displayList);
  612.     free (sizeArray);
  613.     }
  614. }
  615.  
  616. /* Routines to build 3 dimensional solids, including:
  617.  *
  618.  * drawbox, doughnut, icosahedron, 
  619.  * octahedron, tetrahedron, dodecahedron.
  620.  */
  621.  
  622. /* drawbox:
  623.  *
  624.  * draws a rectangular box with the given x, y, and z ranges.  
  625.  * The box is axis-aligned.
  626.  */
  627. void drawbox(GLdouble x0, GLdouble x1, GLdouble y0, GLdouble y1,
  628.     GLdouble z0, GLdouble z1, GLenum type)
  629. {
  630.     static GLdouble n[6][3] = {
  631.     {-1.0, 0.0, 0.0}, {0.0, 1.0, 0.0}, {1.0, 0.0, 0.0},
  632.     {0.0, -1.0, 0.0}, {0.0, 0.0, 1.0}, {0.0, 0.0, -1.0}
  633.     };
  634.     static GLint faces[6][4] = {
  635.     { 0, 1, 2, 3 }, { 3, 2, 6, 7 }, { 7, 6, 5, 4 },
  636.     { 4, 5, 1, 0 }, { 5, 6, 2, 1 }, { 7, 4, 0, 3 }
  637.     };
  638.     GLdouble v[8][3], tmp;
  639.     GLint i;
  640.  
  641.     if (x0 > x1) {
  642.     tmp = x0; x0 = x1; x1 = tmp;
  643.     }
  644.     if (y0 > y1) {
  645.     tmp = y0; y0 = y1; y1 = tmp; 
  646.     }
  647.     if (z0 > z1) {
  648.     tmp = z0; z0 = z1; z1 = tmp; 
  649.     }
  650.     v[0][0] = v[1][0] = v[2][0] = v[3][0] = x0;
  651.     v[4][0] = v[5][0] = v[6][0] = v[7][0] = x1;
  652.     v[0][1] = v[1][1] = v[4][1] = v[5][1] = y0;
  653.     v[2][1] = v[3][1] = v[6][1] = v[7][1] = y1;
  654.     v[0][2] = v[3][2] = v[4][2] = v[7][2] = z0;
  655.     v[1][2] = v[2][2] = v[5][2] = v[6][2] = z1;
  656.  
  657.     for (i = 0; i < 6; i++) {
  658.     glBegin(type);
  659.     glNormal3dv(&n[i][0]);
  660.     glVertex3dv(&v[faces[i][0]][0]);
  661.     glNormal3dv(&n[i][0]);
  662.     glVertex3dv(&v[faces[i][1]][0]);
  663.     glNormal3dv(&n[i][0]);
  664.     glVertex3dv(&v[faces[i][2]][0]);
  665.     glNormal3dv(&n[i][0]);
  666.     glVertex3dv(&v[faces[i][3]][0]);
  667.     glEnd();
  668.     }
  669. }
  670.  
  671. /* doughnut:
  672.  *
  673.  * draws a doughnut, centered at (0, 0, 0) whose axis is aligned with
  674.  * the z-axis.  The doughnut's major radius is R, and minor radius is r.
  675.  */
  676.  
  677. void doughnut(GLdouble r, GLdouble R, GLint nsides, GLint rings, GLenum type)
  678. {
  679.     int    i, j;
  680.     GLdouble    theta, phi, theta1, phi1;
  681.     GLdouble    p0[03], p1[3], p2[3], p3[3];
  682.     GLdouble    n0[3], n1[3], n2[3], n3[3];
  683.  
  684.     for (i = 0; i < rings; i++) {
  685.     theta = (GLdouble)i*2.0*PI/rings;
  686.     theta1 = (GLdouble)(i+1)*2.0*PI/rings;
  687.     for (j = 0; j < nsides; j++) {
  688.         phi = (GLdouble)j*2.0*PI/nsides;
  689.         phi1 = (GLdouble)(j+1)*2.0*PI/nsides;
  690.  
  691.         p0[0] = cos(theta)*(R + r*cos(phi));
  692.         p0[1] = -sin(theta)*(R + r*cos(phi));
  693.         p0[2] = r*sin(phi);
  694.  
  695.         p1[0] = cos(theta1)*(R + r*cos(phi));
  696.         p1[1] = -sin(theta1)*(R + r*cos(phi));
  697.         p1[2] = r*sin(phi);
  698.  
  699.         p2[0] = cos(theta1)*(R + r*cos(phi1));
  700.         p2[1] = -sin(theta1)*(R + r*cos(phi1));
  701.         p2[2] = r*sin(phi1);
  702.  
  703.         p3[0] = cos(theta)*(R + r*cos(phi1));
  704.         p3[1] = -sin(theta)*(R + r*cos(phi1));
  705.         p3[2] = r*sin(phi1);
  706.  
  707.         n0[0] = cos(theta)*(cos(phi));
  708.         n0[1] = -sin(theta)*(cos(phi));
  709.         n0[2] = sin(phi);
  710.  
  711.         n1[0] = cos(theta1)*(cos(phi));
  712.         n1[1] = -sin(theta1)*(cos(phi));
  713.         n1[2] = sin(phi);
  714.  
  715.         n2[0] = cos(theta1)*(cos(phi1));
  716.         n2[1] = -sin(theta1)*(cos(phi1));
  717.         n2[2] = sin(phi1);
  718.  
  719.         n3[0] = cos(theta)*(cos(phi1));
  720.         n3[1] = -sin(theta)*(cos(phi1));
  721.         n3[2] = sin(phi1);
  722.  
  723.         m_xformpt(p0, p0, n0, n0);
  724.         m_xformpt(p1, p1, n1, n1);
  725.         m_xformpt(p2, p2, n2, n2);
  726.         m_xformpt(p3, p3, n3, n3);
  727.  
  728.         glBegin(type);
  729.         glNormal3dv(n3);
  730.         glVertex3dv(p3);
  731.         glNormal3dv(n2);
  732.         glVertex3dv(p2);
  733.         glNormal3dv(n1);
  734.         glVertex3dv(p1);
  735.         glNormal3dv(n0);
  736.         glVertex3dv(p0);
  737.         glEnd();
  738.     }
  739.     }
  740. }
  741.  
  742. /* octahedron data: The octahedron produced is centered 
  743.  * at the origin and has radius 1.0 
  744.  */
  745. static GLdouble odata[6][3] = {
  746.   {1.0, 0.0, 0.0},
  747.   {-1.0, 0.0, 0.0},
  748.   {0.0, 1.0, 0.0},
  749.   {0.0, -1.0, 0.0},
  750.   {0.0, 0.0, 1.0},
  751.   {0.0, 0.0, -1.0}
  752. };
  753.  
  754. static int ondex[8][3] = {
  755.     {0, 4, 2}, {1, 2, 4}, {0, 3, 4}, {1, 4, 3},
  756.     {0, 2, 5}, {1, 5, 2}, {0, 5, 3}, {1, 3, 5}
  757. };
  758.  
  759. /* tetrahedron data: */
  760.  
  761. #define T    1.73205080756887729
  762.  
  763. static GLdouble tdata[4][3] = {
  764.     {T, T, T}, {T, -T, -T}, {-T, T, -T}, {-T, -T, T}
  765. };
  766.  
  767. static int tndex[4][3] = {
  768.     {0, 1, 3}, {2, 1, 0}, {3, 2, 0}, {1, 2, 3}
  769. };
  770.  
  771. /* icosahedron data: These numbers are rigged to 
  772.  * make an icosahedron of radius 1.0 
  773.  */
  774.  
  775. #define X .525731112119133606
  776. #define Z .850650808352039932
  777.  
  778. static GLdouble idata[12][3] = {
  779.   {-X, 0, Z},
  780.   {X, 0, Z},
  781.   {-X, 0, -Z},
  782.   {X, 0, -Z},
  783.   {0, Z, X},
  784.   {0, Z, -X},
  785.   {0, -Z, X},
  786.   {0, -Z, -X},
  787.   {Z, X, 0},
  788.   {-Z, X, 0},
  789.   {Z, -X, 0},
  790.   {-Z, -X, 0}
  791. };
  792.  
  793. static int index[20][3] = {
  794.     {0, 4, 1},    {0, 9, 4},
  795.     {9, 5, 4},    {4, 5, 8},
  796.     {4, 8, 1},    {8, 10, 1},
  797.     {8, 3, 10},    {5, 3, 8},
  798.     {5, 2, 3},    {2, 7, 3},
  799.     {7, 10, 3},    {7, 6, 10},
  800.     {7, 11, 6},    {11, 0, 6},
  801.     {0, 1, 6},    {6, 1, 10},
  802.     {9, 0, 11},    {9, 11, 2},
  803.     {9, 2, 5},    {7, 2, 11},
  804. };
  805.  
  806. /* icosahedron:
  807.  *
  808.  * Draws an icosahedron with center at p0 having the
  809.  * given radius.
  810.  */
  811.  
  812. static void icosahedron(GLdouble p0[3], GLdouble radius, GLenum shadeType)
  813. {
  814.     int i;
  815.  
  816.     for (i = 0; i < 20; i++)
  817.     drawtriangle(i, 0, 1, p0, radius, shadeType, 0);
  818. }
  819.  
  820. /* octahedron:
  821.  *
  822.  * Draws an octahedron with center at p0 having the
  823.  * given radius.
  824.  */
  825. static void octahedron(GLdouble p0[3], GLdouble radius, GLenum shadeType)
  826. {
  827.     int i;
  828.  
  829.     for (i = 0; i < 8; i++)
  830.     drawtriangle(i, 1, 1, p0, radius, shadeType, 0);
  831. }
  832.  
  833. /* tetrahedron:
  834.  *
  835.  * Draws an tetrahedron with center at p0 having the
  836.  * given radius.
  837.  */
  838.  
  839. static void tetrahedron(GLdouble p0[3], GLdouble radius, GLenum shadeType)
  840. {
  841.     int i;
  842.  
  843.     for (i = 0; i < 4; i++)
  844.     drawtriangle(i, 2, 1, p0, radius, shadeType, 0);
  845. }
  846.  
  847. static void subdivide(int depth, GLdouble *v0, GLdouble *v1, GLdouble *v2,
  848.     GLdouble p0[3], GLdouble radius, GLenum shadeType, int avnormal)
  849. {
  850.     GLdouble w0[3], w1[3], w2[3];
  851.     GLdouble l;
  852.     int i, j, k, n;
  853.  
  854.     for (i = 0; i < depth; i++)
  855.     for (j = 0; i + j < depth; j++) {
  856.         k = depth - i - j;
  857.         for (n = 0; n < 3; n++) {
  858.         w0[n] = (i*v0[n] + j*v1[n] + k*v2[n])/depth;
  859.         w1[n] = ((i+1)*v0[n] + j*v1[n] + (k-1)*v2[n])/depth;
  860.         w2[n] = (i*v0[n] + (j+1)*v1[n] + (k-1)*v2[n])/depth;
  861.         }
  862.         l = sqrt(w0[0]*w0[0] + w0[1]*w0[1] + w0[2]*w0[2]);
  863.         w0[0] /= l; w0[1] /= l; w0[2] /= l;
  864.         l = sqrt(w1[0]*w1[0] + w1[1]*w1[1] + w1[2]*w1[2]);
  865.         w1[0] /= l; w1[1] /= l; w1[2] /= l;
  866.         l = sqrt(w2[0]*w2[0] + w2[1]*w2[1] + w2[2]*w2[2]);
  867.         w2[0] /= l; w2[1] /= l; w2[2] /= l;
  868.         recorditem(w1, w0, w2, p0, radius, shadeType, avnormal);
  869.     }
  870.     for (i = 0; i < depth-1; i++)
  871.     for (j = 0; i + j < depth-1; j++) {
  872.         k = depth - i - j;
  873.         for (n = 0; n < 3; n++) {
  874.         w0[n] = ((i+1)*v0[n] + (j+1)*v1[n] + (k-2)*v2[n])/depth;
  875.         w1[n] = ((i+1)*v0[n] + j*v1[n] + (k-1)*v2[n])/depth;
  876.         w2[n] = (i*v0[n] + (j+1)*v1[n] + (k-1)*v2[n])/depth;
  877.         }
  878.         l = sqrt(w0[0]*w0[0] + w0[1]*w0[1] + w0[2]*w0[2]);
  879.         w0[0] /= l; w0[1] /= l; w0[2] /= l;
  880.         l = sqrt(w1[0]*w1[0] + w1[1]*w1[1] + w1[2]*w1[2]);
  881.         w1[0] /= l; w1[1] /= l; w1[2] /= l;
  882.         l = sqrt(w2[0]*w2[0] + w2[1]*w2[1] + w2[2]*w2[2]);
  883.         w2[0] /= l; w2[1] /= l; w2[2] /= l;
  884.         recorditem(w0, w1, w2, p0, radius, shadeType, avnormal);
  885.     }
  886. }
  887.  
  888. static void drawtriangle(int i, int geomType, int depth,
  889.     GLdouble p0[3], GLdouble radius, GLenum shadeType, int avnormal)
  890. {
  891.     GLdouble *x0, *x1, *x2;
  892.  
  893.     switch (geomType) {
  894.     case 0:    /* icosahedron */
  895.         x0 = &idata[index[i][0]][0];
  896.         x1 = &idata[index[i][1]][0];
  897.         x2 = &idata[index[i][2]][0];
  898.         break;
  899.     case 1: /* octahedron */
  900.         x0 = &odata[ondex[i][0]][0];
  901.         x1 = &odata[ondex[i][1]][0];
  902.         x2 = &odata[ondex[i][2]][0];
  903.         break;
  904.     case 2: /* tetrahedron */
  905.         x0 = &tdata[tndex[i][0]][0];
  906.         x1 = &tdata[tndex[i][1]][0];
  907.         x2 = &tdata[tndex[i][2]][0];
  908.         break;
  909.     }
  910.     subdivide(depth, x0, x1, x2, p0, radius, shadeType, avnormal);
  911. }
  912.  
  913. static void recorditem(GLdouble *n1, GLdouble *n2, GLdouble *n3,
  914.     GLdouble center[3], GLdouble radius, GLenum shadeType, int avnormal)
  915. {
  916.     GLdouble p1[3], p2[3], p3[3], q0[3], q1[3], n11[3], n22[3], n33[3];
  917.     int    i;
  918.  
  919.     for (i = 0; i < 3; i++) {
  920.     p1[i] = n1[i]*radius + center[i];
  921.     p2[i] = n2[i]*radius + center[i];
  922.     p3[i] = n3[i]*radius + center[i];
  923.     }
  924.     if (avnormal == 0) {
  925.     diff3(p1, p2, q0);
  926.     diff3(p2, p3, q1);
  927.     crossprod(q0, q1, q1);
  928.     normalize(q1);
  929.     m_xformpt(p1, p1, q1, n11);
  930.     m_xformptonly(p2, p2);
  931.     m_xformptonly(p3, p3);
  932.  
  933.     glBegin (shadeType);
  934.     glNormal3dv(n11);
  935.     glVertex3dv(p1);
  936.     glVertex3dv(p2);
  937.     glVertex3dv(p3);
  938.     glEnd();
  939.     return;
  940.     }
  941.     m_xformpt(p1, p1, n1, n11);
  942.     m_xformpt(p2, p2, n2, n22);
  943.     m_xformpt(p3, p3, n3, n33);
  944.  
  945.     glBegin (shadeType);
  946.     glNormal3dv(n11);
  947.     glVertex3dv(p1);
  948.     glNormal3dv(n22);
  949.     glVertex3dv(p2);
  950.     glNormal3dv(n33);
  951.     glVertex3dv(p3);
  952.     glEnd();
  953. }
  954.  
  955. static GLdouble dodec[20][3];
  956.  
  957. static void initdodec()
  958. {
  959.     GLdouble alpha, beta;
  960.  
  961.     alpha = sqrt(2.0/(3.0 + sqrt(5.0)));
  962.     beta = 1.0 + sqrt(6.0/(3.0 + sqrt(5.0)) - 2.0 + 2.0*sqrt(2.0/(3.0 +
  963.                                 sqrt(5.0))));
  964.     dodec[0][0] = -alpha; dodec[0][1] = 0; dodec[0][2] = beta;
  965.     dodec[1][0] = alpha; dodec[1][1] = 0; dodec[1][2] = beta;
  966.     dodec[2][0] = -1; dodec[2][1] = -1; dodec[2][2] = -1;
  967.     dodec[3][0] = -1; dodec[3][1] = -1; dodec[3][2] = 1;
  968.     dodec[4][0] = -1; dodec[4][1] = 1; dodec[4][2] = -1;
  969.     dodec[5][0] = -1; dodec[5][1] = 1; dodec[5][2] = 1;
  970.     dodec[6][0] = 1; dodec[6][1] = -1; dodec[6][2] = -1;
  971.     dodec[7][0] = 1; dodec[7][1] = -1; dodec[7][2] = 1;
  972.     dodec[8][0] = 1; dodec[8][1] = 1; dodec[8][2] = -1;
  973.     dodec[9][0] = 1; dodec[9][1] = 1; dodec[9][2] = 1;
  974.     dodec[10][0] = beta; dodec[10][1] = alpha; dodec[10][2] = 0;
  975.     dodec[11][0] = beta; dodec[11][1] = -alpha; dodec[11][2] = 0;
  976.     dodec[12][0] = -beta; dodec[12][1] = alpha; dodec[12][2] = 0;
  977.     dodec[13][0] = -beta; dodec[13][1] = -alpha; dodec[13][2] = 0;
  978.     dodec[14][0] = -alpha; dodec[14][1] = 0; dodec[14][2] = -beta;
  979.     dodec[15][0] = alpha; dodec[15][1] = 0; dodec[15][2] = -beta;
  980.     dodec[16][0] = 0; dodec[16][1] = beta; dodec[16][2] = alpha;
  981.     dodec[17][0] = 0; dodec[17][1] = beta; dodec[17][2] = -alpha;
  982.     dodec[18][0] = 0; dodec[18][1] = -beta; dodec[18][2] = alpha;
  983.     dodec[19][0] = 0; dodec[19][1] = -beta; dodec[19][2] = -alpha;
  984. }
  985.  
  986. /* dodecahedron:
  987.  *
  988.  * Draws an dodecahedron with center at 0.0. The radius
  989.  * is sqrt(3).
  990.  */
  991. static void dodecahedron(GLdouble center[3], GLdouble sc, GLenum type)
  992. {
  993.     static int inited = 0;
  994.  
  995.     if ( inited == 0) {
  996.     inited = 1;
  997.     initdodec();
  998.     }
  999.     m_pushmatrix();
  1000.     m_translate(center[0], center[1], center[2]);
  1001.     m_scale(sc, sc, sc);
  1002.     pentagon(0, 1, 9, 16, 5, type);
  1003.     pentagon(1, 0, 3, 18, 7, type);
  1004.     pentagon(1, 7, 11, 10, 9, type);
  1005.     pentagon(11, 7, 18, 19, 6, type);
  1006.     pentagon(8, 17, 16, 9, 10, type);
  1007.     pentagon(2, 14, 15, 6, 19, type);
  1008.     pentagon(2, 13, 12, 4, 14, type);
  1009.     pentagon(2, 19, 18, 3, 13, type);
  1010.     pentagon(3, 0, 5, 12, 13, type);
  1011.     pentagon(6, 15, 8, 10, 11, type);
  1012.     pentagon(4, 17, 8, 15, 14, type);
  1013.     pentagon(4, 12, 5, 16, 17, type);
  1014.     m_popmatrix();
  1015. }
  1016.  
  1017. static void pentagon(int a, int b, int c, int d, int e, GLenum shadeType)
  1018. {
  1019.     GLdouble n0[3], d1[3], d2[3], d3[3], d4[3], d5[3], nout[3];
  1020.  
  1021.     diff3(&dodec[a][0], &dodec[b][0], d1);
  1022.     diff3(&dodec[b][0], &dodec[c][0], d2);
  1023.     crossprod(d1, d2, n0);
  1024.     normalize(n0);
  1025.     m_xformpt(&dodec[a][0], d1, n0, nout);
  1026.     m_xformptonly(&dodec[b][0], d2);
  1027.     m_xformptonly(&dodec[c][0], d3);
  1028.     m_xformptonly(&dodec[d][0], d4);
  1029.     m_xformptonly(&dodec[e][0], d5);
  1030.  
  1031.     glBegin (shadeType);
  1032.     glNormal3dv(nout);
  1033.     glVertex3dv(d1);
  1034.     glVertex3dv(d2);
  1035.     glVertex3dv(d3);
  1036.     glVertex3dv(d4);
  1037.     glVertex3dv(d5);
  1038.     glEnd();
  1039. }
  1040.  
  1041. /*    linked lists--display lists for each different 
  1042.  *    type of geometric objects.  The linked list is 
  1043.  *    searched, until an object of the requested
  1044.  *    size is found.  If no geometric object of that size
  1045.  *    has been previously made, a new one is created.
  1046.  */
  1047. GLuint findList (int index, GLdouble *paramArray, int size) 
  1048. {
  1049.     MODELPTR endList;
  1050.     int found = 0;
  1051.     
  1052.     endList = lists[index];
  1053.     while (endList != NULL) {
  1054.     if (compareParams (endList->params, paramArray, size))
  1055.         return (endList->list);
  1056.     endList = endList->ptr;
  1057.     }
  1058. /*  if not found, return 0 and calling routine should
  1059.  *  make a new list    
  1060.  */
  1061.     return (0);
  1062. }
  1063.  
  1064. int compareParams (GLdouble *oneArray, GLdouble *twoArray, int size) 
  1065. {
  1066.     int i;
  1067.     int matches = 1;
  1068.  
  1069.     for (i = 0; (i < size) && matches; i++) {
  1070.     if (*oneArray++ != *twoArray++)
  1071.         matches = 0;
  1072.     }
  1073.     return (matches);
  1074. }
  1075.  
  1076. GLuint makeModelPtr (int index, GLdouble *sizeArray, int count)
  1077. {
  1078.     MODELPTR newModel;
  1079.  
  1080.     newModel = (MODELPTR) malloc (sizeof (MODEL));
  1081.     newModel->list = glGenLists (1);
  1082.     newModel->numParam = count;
  1083.     newModel->params = sizeArray;
  1084.     newModel->ptr = lists[index];
  1085.     lists[index] = newModel;
  1086.  
  1087.     return (newModel->list);
  1088. }
  1089.